import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/books.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/models/book/BookFolders.dart';
import 'package:hive_flutter/hive_flutter.dart';

// ignore: must_be_immutable
class BookFolderDropDown extends StatelessWidget {
  BookFolderDropDown({Key? key, required this.withBookNum}) : super(key: key);

  bool withBookNum;

  @override
  Widget build(BuildContext context) {
    final BooksController _bookscontroller = Get.find();
    final box = Hive.box('appconfig');

    _bookscontroller.fetchBookAllFolders();

    bool isTeacher = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Teacher;

    return Obx(() {
      if (_bookscontroller.bookFolders.length > 0) {
        return Container(
          width: 250,
          child: DropdownButtonFormField(
              value: _bookscontroller.bookFolders[0],
              decoration: InputDecoration(
                labelText:
                    isTeacher ? 'Choose Lesson Plan'.tr : 'Choose Folder'.tr,
              ),
              items: _bookscontroller.bookFolders
                  .map((folder) => DropdownMenuItem(
                        value: folder,
                        child: withBookNum
                            ? Text(
                                '${folder.folderName}' +
                                    ' - ' +
                                    '${folder.bookCount}',
                                style: Theme.of(context).textTheme.titleSmall,
                                overflow: TextOverflow.ellipsis,
                              )
                            : Text(
                                '${folder.folderName}',
                                style: Theme.of(context).textTheme.titleSmall,
                                overflow: TextOverflow.ellipsis,
                              ),
                      ))
                  .toList(),
              onChanged: (value) {
                var fd = value as AllFolder;
                if (fd.id != null)
                  _bookscontroller.fetchBookAllFoldersByID(fd.id);
              }),
        );
      } else {
        return Text('');
      }
    });
  }
}
